<?php
class mg_saran extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('mmg_saran');
		$this->load->library('email');
	}
	
	function index()
	{
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->mmg_saran->getList();
			$this->load->view('mg_saran/list',$data);
		}else{
			redirect('admin');
		}
	}
    
	function add(){
		$data['mode'] = "Create";
		$data['action'] = "save";
		$this->load->view('mg_saran/form',$data);
	}
	
	function edit($id_menu){
		$data['mode'] = "Edit";
		$data['action'] = "update";
		$data['detail'] = $this->mmg_saran->detail($id_menu);
		$this->load->view('mg_saran/form',$data);
	}
    
	function save()
	{
		$id_saran = $this->input->post('id_saran');
		$nama_saran = $this->input->post('nama_saran');
		$email_saran = $this->input->post('email_saran');
		$subjek_saran = $this->input->post('subjek_saran');
		$pesan_saran = $this->input->post('pesan_saran');
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mmg_saran->setData($id_saran,$nama_saran,$email_saran,$subjek_saran,$pesan_saran);
			$this->mmg_saran->create();
			$this->session->set_flashdata('success', true);
			redirect('mg_saran');
		}
	}
	
	function front()
	{
		$id_menu = $this->input->post('id_menu');
		$id_saran = $this->input->post('id_saran');
		$nama_saran = $this->input->post('nama_saran');
		$email_saran = $this->input->post('email_saran');
		$subjek_saran = $this->input->post('subjek_saran');
		$pesan_saran = $this->input->post('pesan_saran');
		
		$security_code = $this->input->post('secutity_code');
		$CodeCaptcha = $this->session->userdata('mycaptcha');
		
		$submit = $this->input->post('submit');	
		if ($submit && $security_code == $CodeCaptcha)
		{
			$this->mmg_saran->setData($id_saran,$nama_saran,$email_saran,$subjek_saran,$pesan_saran);
			$this->mmg_saran->create();
			
			// sending to email
			$this->email->clear();
			
			$config['protocol']    = 'smtp';
			$config['smtp_host']    = 'ssl://smtp.mail.yahoo.com';
			$config['smtp_port']    = '465';
			$config['smtp_timeout'] = '7';
			$config['smtp_user']    = 'dead_eyeee@yahoo.com';
			$config['smtp_pass']    = '7Bionic.';
			$config['charset']    = 'utf-8';
			$config['newline']    = "\r\n";
			$config['mailtype'] = 'text'; // or html
			$config['validation'] = TRUE; // bool whether to validate email or not      

			$this->email->initialize($config);
			$this->email->from($email_saran, 'Saran Mail');
			$this->email->to('hanif.kharismadini.04@gmail.com');
			
			$this->email->subject($nama_saran .' : '. $subjek_saran);
			$this->email->message($pesan_saran);
			$this->email->send();
			
			$this->session->set_flashdata('success', true);
			redirect('homepage/content/'.$id_menu);
		}else{
			$this->session->set_flashdata('error', true);
			redirect('homepage/content/'.$id_menu);
		}
	}
	
	function update()
	{
		$id_saran = $this->input->post('id_saran');
		$nama_saran = $this->input->post('nama_saran');
		$email_saran = $this->input->post('email_saran');
		$subjek_saran = $this->input->post('subjek_saran');
		$pesan_saran = $this->input->post('pesan_saran');
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mmg_saran->setData($id_saran,$nama_saran,$email_saran,$subjek_saran,$pesan_saran);
			$this->mmg_saran->update($id_saran);
			$this->session->set_flashdata('success', true);
			redirect('mg_saran');
		}
	}
	
	function delete($id_saran)
	{
		if ($this->mmg_saran->remove($id_saran)){
			$this->session->set_flashdata('delete', true);
			redirect('mg_saran','refresh');
		}	
	}	
}
?>